package com.practice.concurrency.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadTypeMain {

    static class UserThread extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                System.out.println("This is a user thread....." + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class DaemonThread extends Thread {
        public DaemonThread() {
            setDaemon(true);
        }

        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                System.out.println("This is daemon thread....." + i);
                /*try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
        }
    }

    public static void main(String[] args) {
    }

    private static void startDaemonThread() {

        DaemonThread daemon = new DaemonThread();   //Creating the DaemonThread

        daemon.start();                 //Starting the daemon thread

        UserThread userThread = new UserThread();   //Creating the UserThread

        userThread.start();          //Starting the user thread

        System.out.println("Main thread done.");
    }

    private static void startThreadPool() {

        Callable<Integer> callee = () -> {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(5000);
            System.out.println("Callee done.");
            return 5000;
        };

        // FutureTask<Integer> task = new FutureTask<>(callee);

        // new Thread(task).start();

        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(callee); // Thread in executor is daemon thread.
        executor.submit(() -> executor.shutdown());


        System.out.println("Caller done.");
    }

}
